﻿using MKAdminCore.DTO.Web.Common;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace MKAdminCore.DTO.CusAttribute
{
    /// <summary>
    /// 验证属性是否必填
    /// </summary>
    public class CusRequiredAttribute : AbstractCusAttribute
    {
        /// <summary>
        /// 验证是否为空
        /// </summary>
        /// <param name="oValue"></param>
        /// <returns></returns>
        public override HttpResponseResult Validate<T>(T dto, PropertyInfo prop)
        {
            var result = new HttpResponseResult();
            //获取属性内容
            object oValue = prop.GetValue(dto);
            //获取属性类型
            var propertyType = prop.PropertyType.Name.ToString().ToLower();
            switch (propertyType)
            {
                case "int32":
                case "int64":
                    result.Code = (Convert.ToInt64(oValue) > 0) ? 200 : 400;
                    break;
                default:
                    result.Code = (oValue != null && !string.IsNullOrWhiteSpace(oValue.ToString())) ? 200 : 400;
                    break;
            }
            if (result.Code != 200)
            {
                result.Msg = base.ErrorMsg;
            }

            return result;
        }
    }
}
